package com.atguigu.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.edu.entity.Subject;
import com.atguigu.edu.entity.subject.OneSubject;
import com.atguigu.edu.entity.subject.TwoSubject;
import com.atguigu.edu.excel.SubjectData;
import com.atguigu.edu.mapper.SubjectMapper;
import com.atguigu.edu.service.SubjectService;
import com.atguigu.edu.listener.SubjectExcelListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author ld
 * @since 2022-08-15
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public void addSubject(MultipartFile file, SubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            throw new CustomException(20001,"添加课程分类失败");
        }

    }

    @Override
    public List<OneSubject> listByTree() {
        List<OneSubject> finalSubject=new ArrayList<>();
        //1.获取一级分类
        QueryWrapper<Subject> oneQueryWrapper=new QueryWrapper<>();
        oneQueryWrapper.eq("parent_id",0);
        List<Subject> oneSubjectList = baseMapper.selectList(oneQueryWrapper);

        //2.获取二级分类
//        QueryWrapper<Subject> twoQueryWrapper=new QueryWrapper<>();
//        twoQueryWrapper.ne("parent_id",0);
//        List<Subject> twoSubjectList = baseMapper.selectList(twoQueryWrapper);
        
        //3.将一级分类和二级分类合并
//        for (int i = 0; i < oneSubjectList.size(); i++) {
//            Subject subject=oneSubjectList.get(i);
//            OneSubject oneSubject=new OneSubject();
//            BeanUtils.copyProperties(subject,oneSubject);
//            List<TwoSubject> tSubjectList=new ArrayList<>();
//            for (int j = 0; j < twoSubjectList.size(); j++) {
//                if(twoSubjectList.get(j).getParentId().equals(subject.getId())){
//                    TwoSubject twoSubject=new TwoSubject();
//                    BeanUtils.copyProperties(twoSubjectList.get(j),twoSubject);
//                    tSubjectList.add(twoSubject);
//                }
//            }
//            //把一级下面所有的二级分类放到一级分类里面
//            oneSubject.setChildren(tSubjectList);
//            finalSubject.add(oneSubject);
//        }
        finalSubject=oneSubjectList.stream().map(
                item->{
                    OneSubject oneSubject=new OneSubject();
                    BeanUtils.copyProperties(item,oneSubject);
                    LambdaQueryWrapper<Subject> queryWrapper=new LambdaQueryWrapper<>();
                    queryWrapper.eq(Subject::getParentId,item.getId());
                    //得到二级分类Subject
                    List<Subject> twoSubjectList = baseMapper.selectList(queryWrapper);
                    List<TwoSubject> twoSubjects=new ArrayList<>();
                    //将二级分类Subject 转变为 二级分类TwoSubject
                    for(Subject two : twoSubjectList){
                        TwoSubject twoSubject=new TwoSubject();
                        BeanUtils.copyProperties(two,twoSubject);
                        twoSubjects.add(twoSubject);
                    }
                    oneSubject.setChildren(twoSubjects);
                    return oneSubject;
                }
        ).collect(Collectors.toList());
        return finalSubject;
    }
}
